import mysql.connector
#性别分布
def genderRatio(sum):
    # 打开数据库连接
    db = mysql.connector.connect(
        host="124.222.244.117",  # 数据库主机地址
        user="zrgj9",  # 用户名
        passwd="zrgj9",  # 密码
        database="zrgj9"
    )
    # 使用cursor()方法获取操作游标
    mycursor = db.cursor()
    # SQL 查询语句
    sql = """SELECT gender,
            count(stuid) 该性别人数
            from student
            where register = 1
            group by gender"""
    try:
        # 执行sql语句
        mycursor.execute(sql)
        data = mycursor.fetchall()
        # 提交到数据库执行
        db.commit()
    except:
        # 如果发生错误则回滚
        db.rollback()

    # 将元组列表转换成二维列表，进行比例换算
    for i in range(len(data)):
        data[i] = list(data[i])
        data[i][1] /= sum

    dict = {}
    for i in range(len(data)):   
        dict[data[i][0]] = round(data[i][1]*100,2)
    dict["Total"]=100
    return dict
